package com.yxt.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yxt.common.utils.JwtUtil;
import com.yxt.user.config.ResourceServerConfig;
import com.yxt.user.dto.UserDto;
import com.yxt.user.entity.UserEntity;
import com.yxt.user.service.UserService;
import com.yxt.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.*;
import com.yxt.common.utils.PageUtils;
import com.yxt.common.utils.R;



/**
 * 系统用户
 *
 * @author august
 * @email 379249906@qq.com
 * @date 2020-07-31 17:49:40
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/hello")
    public String hello(){
        return "hellouser";
    }

    @GetMapping("/hello2")
    public String hello2(){
        return "hellouser";
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
		UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }

    @GetMapping("/info")
    public UserDto findByUserName(String userName) {
        LambdaQueryWrapper<UserEntity> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(userName!=null,UserEntity::getUsername,userName);
        UserEntity userEntity = userService.getOne(userLambdaQueryWrapper);
        UserDto userDto = new UserDto();
        userDto.setPassword(userEntity.getPassword());
        userDto.setUsername(userName);
        return userDto;
    }

    @GetMapping("/userInfo")
    public R getUserInfo(){
//        OAuth2AuthenticationDetails authentication = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
//        Map<String, String> stringStringMap = JwtUtil.decodeToken(authentication.getTokenValue(), ResourceServerConfig.PUBLIC_KEY);
//
//        String userName = stringStringMap.get("user_name");
//
//
//        return R.ok().put("data",stringStringMap);
        return userService.getUserInfo();

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
        String encodePWD = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePWD);
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
